package com.hibikiluler.javalearning.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.hibikiluler.javalearning.util.StringUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 用户实体
 * 	id bigint(20) not null primary key auto_increment comment '主键',
 * 	user_name varchar(20) comment '用户名',
 * 	password varchar(20) comment '密码',
 * 	nick_name varchar(50) comment '昵称',
 * 	sex tinyint comment '性别（男/女）',
 * 	avatar varchar(100) comment '用户头像',
 * 	address varchar(100) comment '地址',
 * 	open_id varchar(100) comment '微信小程序openid，每个用户唯一',
 * 	status tinyint(1) comment '状态是否禁用',
 * 	admin tinyint(1) comment '是否是管理员',
 * 	phone_number varchar(20) comment '电话号码'
 */
@Data
public class SysUser implements UserDetails {
    @ApiModelProperty(value = "主键")
    private long id;  //用户ID
    @ApiModelProperty(value = "前端展示的用户名")
    private String name;
    @ApiModelProperty(value = "用户名")
    private String userName; //用户名
    @ApiModelProperty(value = "密码")
    private String password; //密码
    @ApiModelProperty(value = "昵称")
    private String nickName; //昵称
    @ApiModelProperty(value = "性别（男/女）")
    private Integer sex; //性别（男/女）
    @ApiModelProperty(value = "用户头像")
    private String avatar; //用户头像
    @ApiModelProperty(value = "地址")
    private String address; //地址
    @ApiModelProperty(value = "微信小程序openid")
    private String openId; //微信小程序openid
    @ApiModelProperty(value = "状态是否禁用")
    private boolean status; //状态是否禁用
    @ApiModelProperty(value = "是否是管理员")
    private boolean admin; //是否是管理员
    @ApiModelProperty(value = "电话")
    private String phoneNumber; //电话
    @ApiModelProperty(value = "用户邮箱")
    private String email; //用户邮箱
    @ApiModelProperty(value = "用户角色信息")
    private List<SysRole> roles; //用户角色信息
    @ApiModelProperty(value = "用户对应的菜单列表")
    private List<SysMenus> menuMap; //角色对应的菜单列表
    @ApiModelProperty(value = "用户对应的数据权限")
    private List<SysAdmins> adminMap; //角色对应的数据权限

    @Override           //权限数据
    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> list = new ArrayList<>();
        if (roles != null && roles.size() > 0) {
            roles.forEach(item -> {
                if (StringUtils.isNotEmpty(item.getCodes())) {
                    //将角色信息和权限拼接
                    list.add(new SimpleGrantedAuthority("ROLE_" + item.getCodes()));
                }
            });
        }
        if (adminMap != null && adminMap.size() > 0){
            adminMap.forEach(item -> {
                if (item.getCodes() != null) {
                    list.add(new SimpleGrantedAuthority(item.getCodes()));
                }
            });
        }
        return list;
    }

    @Override     //密码
    public String getPassword() {
        return password;
    }

    @Override    //用户名
    public String getUsername() {
        return userName;
    }

    @Override       //账号是否过期
    public boolean isAccountNonExpired() {
        return false;
    }

    @Override          //账号是否锁定
    public boolean isAccountNonLocked() {
        return false;
    }

    @Override           //当前账号证书（密码）是否过期
    public boolean isCredentialsNonExpired() {
        return false;
    }

    @Override         //是否被禁用
    public boolean isEnabled() {
        return status;
    }
}
